Separation of Concern
import java.sql.Connection; import java.sql.Statement; import java.sql.DriverManager; class Student{ String name; int roll; public Student(String name, int roll){ this.name = name; this.roll = roll; } public String getName(){ return name; } public int getRoll(){ return roll; } public void show(){ System.out.println("Name: "+name+"\nRoll: "+roll); } } class Conn{ Connection c; public Conn(String jdbc,String user,String pwd){ try{ c = DriverManager.getConnection(jdbc,user,pwd); }catch(Exception e){ System.out.println("Connection Creating Error: "+e.getMessage()); } } public Connection getConn() throws Exception{ return c; } } class DataModel{ Conn c; Connection con; Statement stmt; public DataModel() throws Exception{ try{ c =new Conn("jdbc:mysql://localhost/test","root",""); con = c.getConn(); stmt = con.createStatement(); }catch(Exception iie){ System.out.println("Connection Error: "+iie.getMessage()); } } public String makepayment(fee f) throws Exception { Student s = f.getStudent(); int fee_amount = f.getFeeAmount(); String semester = f.getSemester(); String paid_date = f.getpdate(); String sql = "insert into fee_payment(name,roll,fee_amount,semester,paid_date) values"; sql += "('"+s.getName()+"',"+s.getRoll()+","+fee_amount+",'"+semester+"','"+paid_date+"')"; stmt.executeUpdate(sql); return "Payment Made Successfully!!!"; } } class fee{ Student s; int fee_amount; String semester; String paid_date; public fee(Student s, int famt, String sem,String pd){ this.s = s; fee_amount = famt; semester = sem; paid_date = pd; } public Student getStudent(){ return s; } public int getFeeAmount(){ return fee_amount; } public String getSemester(){ return semester; } public String getpdate(){ return paid_date; } public void getFeeInfo(){ s.show(); System.out.println("\nFee Amount: "+fee_amount+"\nSemester:"+semester+"\nPaid Date: "+paid_date); } } import javax.swing.*; import java.awt.*; import java.awt.event.*; class feeUI extends JFrame implements ActionListener{ JLabel lname,lroll,lsem,lpdate,lpamount; JTextField tname,troll,tsem,tpdate,tpamount; JButton btn; public feeUI(){ setLayout(null); lroll = new JLabel("Roll ");lroll.setBounds(0,10,110,20); lname = new JLabel("Name"); lname.setBounds(20,20,110,22); lsem = new JLabel("Semester"); lsem.setBounds(20,45,110,22); lpdate = new JLabel("Payment Date"); lpdate.setBounds(20,70,110,22); lpamount = new JLabel("Amount"); lpamount.setBounds(20,95,110,22); troll = new JTextField(10);troll.setBounds(130,10,110,20); tname = new JTextField(20); tname.setBounds(130,20,110,22); tsem = new JTextField(20); tsem.setBounds(130,45,110,22); tpdate = new JTextField(20); tpdate.setBounds(130,70,110,22); tpamount = new JTextField(20); tpamount.setBounds(130,95,110,22); btn = new JButton("Make Payment");btn.setBounds(130,120,110,22); add(lroll);add(troll); add(lname);add(tname); add(lsem); add(tsem); add(lpdate);add(tpdate); add(lpamount);add(tpamount); add(btn); btn.addActionListener(this); } public void actionPerformed(ActionEvent aae){ int roll = Integer.parseInt(troll.getText()); int amount = Integer.parseInt(tpamount.getText()); new MakePayment(tname.getText(),roll,tsem.getText(),tpdate.getText(),amount); } } import javax.swing.JFrame; class MakePayment{ fee f; Student s; DataModel dm; public MakePayment(String tname,int roll,String tsem,String tpdate,int tpamount){ s = new Student(tname,roll); f = new fee(s, tpamount,tsem,tpdate); try{ dm = new DataModel(); System.out.println(dm.makepayment(f)); }catch(Exception ie){ System.out.println("Excpetion: "+ie.getMessage()); } } } class feemgmt{ public static void main(String[] oopp){ feeUI f = new feeUI(); f.setSize(400,400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }